<?php
namespace App\Middlewares;

class TrailingSlashMiddleware extends  BaseMiddleware {
	
	public function __invoke($request, $response, $next) {
		$uri = $request -> getUri();
		$path = $uri -> getPath();
		if ($path != '/' && substr($path, -1) == '/') {
			$uri = $uri -> withPath(substr($path, 0, -1));
			return $response -> withRedirect((string)$uri, 301);
			//return $response -> withStatus(301) -> withHeader("Location", $uri);
		}

		return $next($request, $response);
	}

}
?>